package com.banciyuan.bcywebview.biz.circles.relatedcircle;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.event.EventDetailActivity;
import com.banciyuan.bcywebview.utils.file.ImageManager;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.DiscoverData;
import de.greenrobot.daoexample.model.TagDetail;

public class RelatedCircleActivity extends BaseActivity {

    private List<TagDetail> tagDetails = new ArrayList<>();
    private DiscoverData discoverData;

    private TextView mTitle;
    private TextView mIntro;
    private ImageView mHeader;
    private ImageView mback;
    private ImageView mBackground;

    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.activity_grouptaginfo);
        initArgs();
        initUi();
        initAction();
    }


    @Override
    protected void initArgs() {
        tagDetails = (List<TagDetail>) getIntent().getSerializableExtra("relacircle");
        discoverData = (DiscoverData) getIntent().getSerializableExtra("discoverdata");
    }


    @Override
    protected void initUi() {
        mListView = (ListView) findViewById(R.id.listView);

        mTitle = (TextView) findViewById(R.id.rela_title);
        mIntro = (TextView) findViewById(R.id.rela_intro);
        mback = (ImageView) findViewById(R.id.back);
        mHeader = (ImageView) findViewById(R.id.rela_iv);
        mBackground = (ImageView) findViewById(R.id.rela_background);

        if (discoverData != null) {
            ImageLoader.getInstance().displayImage(discoverData.getCover(), mHeader, ImageOptions.options);
            File imagePath = new File(ImageManager.getTemp(this) + "screencircle.jpg");
            if (imagePath.exists()) {
                mBackground.setImageBitmap(BitmapFactory.decodeFile(imagePath.getAbsolutePath()));
            }
            if (!TextUtils.isEmpty(discoverData.getName())) {
                mTitle.setText(discoverData.getName());
            }
            if (!TextUtils.isEmpty(discoverData.getReal_name())) {
                mTitle.setText(discoverData.getReal_name());
            }
            if (!TextUtils.isEmpty(discoverData.getIntro())) {
                mIntro.setText(discoverData.getIntro());
            }
        }
        if (tagDetails != null) {
            mListView.addHeaderView(View.inflate(getContext(), R.layout.rela_circle_label, null));
            RelatedCircleAdapter relatedCircleAdapter = new RelatedCircleAdapter(this, tagDetails);
            mListView.setAdapter(relatedCircleAdapter);
        }
    }

    @Override
    protected void initAction() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int index = position - 1;
                if (index < tagDetails.size()) {
                    TagDetail tagDetail = tagDetails.get(index);
                    if ("work".equals(tagDetail.getType())) {
                        Intent intent = new Intent(RelatedCircleActivity.this, CircleSmoothActivity.class);
                        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, tagDetail.getName());
                        intent.putExtra(gotoUtil.INTENT_VALUE_TWO, tagDetail.getWid());
                        startActivity(intent);
                    } else if ("tag".equals(tagDetail.getType())) {
                        Intent intent = new Intent(RelatedCircleActivity.this, CircleTagSmoothActivity.class);
                        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, tagDetail.getTag_name());
                        intent.putExtra("type", "tag");
                        intent.putExtra("from", 0);
                        startActivity(intent);
                    } else if ("event".equals(tagDetail.getType())) {
                        gotoUtil.gotoAct(RelatedCircleActivity.this, EventDetailActivity.class, tagDetail.getEvent_id());

                    }
                }

            }
        });
        mback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
